package com.jtlrm.ckd.mvp.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.ListView;

import com.base.sdk.base.net.CommonObserver;
import com.hyphenate.chatuidemo.Constant;
import com.hyphenate.chatuidemo.db.NewUserDao;
import com.hyphenate.chatuidemo.domain.NewUserEntity;
import com.hyphenate.chatuidemo.ui.ChatActivity;
import com.jtlrm.ckd.R;
import com.jtlrm.ckd.base.acitvity.TitleBarActivity;
import com.jtlrm.ckd.entity.WorkerEntity;
import com.jtlrm.ckd.mvp.model.SuiFangModel;
import com.jtlrm.ckd.mvp.model.dao.UserHelper;
import com.jtlrm.ckd.util.ContactList.ContactAdapter;
import com.jtlrm.ckd.util.ContactList.ContactListView;
import com.jtlrm.ckd.util.ContactList.SlideBarEntity;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class MyWorkerActivity extends TitleBarActivity {


    @BindView(R.id.my_contact_list)
    ContactListView contactListView;
    protected ListView listView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    SuiFangModel suiFangModel;
    NewUserDao userDao = new NewUserDao();
    List<WorkerEntity> userData = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected String getTitleText() {
        return "我的同事";
    }

    @Override
    protected void setContentLayout() {
        setContentView(R.layout.activity_my_worker);
    }

    @Override
    protected void initView() {
        suiFangModel = new SuiFangModel();
        listView = contactListView.getListView();
    }

    @Override
    protected void obtainData() {

    }

    @Override
    protected void initEvent() {
        smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                loadData();
            }
        });
        smartRefreshLayout.setEnableLoadmore(false);
        smartRefreshLayout.autoRefresh();
    }

    private void loadData() {
        suiFangModel.queryWorkerList(UserHelper.getInstance(context).getUserId()
                , new CommonObserver<List<WorkerEntity>>() {

                    @Override
                    public void onError(int errType, String errMessage) {
                        showToast(errMessage);
                        closeFrshorLoadmore();
                    }

                    @Override
                    public void onResult(List<WorkerEntity> data) {
                        userData.clear();
                        userData.addAll(data);
                        contactListView.init(getSlideList(data));
                        smartRefreshLayout.setEnableLoadmore(false);
                        smartRefreshLayout.setEnableRefresh(false);
                        closeFrshorLoadmore();
                        contactListView.setItemClick(new ContactAdapter.ItemClick() {
                            @Override
                            public void itemClick(SlideBarEntity slideBarEntity) {
                                Intent intent = new Intent(getActivity(), ChatActivity.class);
                                intent.putExtra(Constant.EXTRA_USER_ID, slideBarEntity.getiId());
                                startActivity(intent);
                            }
                        });
                        // 更新本地人员
                        List<NewUserEntity> list = new ArrayList<>();
                        for (WorkerEntity entity : data) {
                            NewUserEntity userEntity = new NewUserEntity();
                            userEntity.setUserId(entity.getId());
                            userEntity.setImId(entity.getEasemobId());
                            userEntity.setAvatar(entity.getHeadPicture());
                            userEntity.setName(entity.getName());
                            userEntity.setRole(entity.getRole()+"");
                            list.add(userEntity);
                        }
                        userDao.saveContactList(list);
                    }
                }, lifecycleSubject);
    }


    public List<SlideBarEntity> getSlideList(List<? extends SlideBarEntity> list) {
        List<SlideBarEntity> data = new ArrayList<>();
        if (list != null) {
            for (SlideBarEntity entity : list) {
                data.add(entity);
            }
        }
        return data;
    }


    /**
     * 关闭加载更多或者刷新
     */
    private void closeFrshorLoadmore() {
        if (smartRefreshLayout.isLoading()) {
            smartRefreshLayout.finishLoadmore();
        }
        if (smartRefreshLayout.isRefreshing()) {
            smartRefreshLayout.finishRefresh();
        }
    }
}
